classdef (StrictDefaults) LiftAPI < matlab.System
    % Tractor lift API: LTSCFastSink(lock,transport,stop,control,fastsink分别代0表锁止，1运输，2停止，3控制，4速降);Damp : 减震；Setpoint：耕深；UpperEnd：上限；Mix：力位综合；DropSpeed：下降速度
    %
    %


    % Public, tunable properties
    properties
    
    end

    % Public, non-tunable properties
    properties (Nontunable)
    OutputBusName = 'bus_name';
    end

    properties (DiscreteState)
    
    CanBusData;
    end

    % Pre-computed constants
    properties (Access = private)

    end

    methods
        % Constructor
        function obj = LiftAPI(varargin)
            % Support name-value pair arguments when constructing object
            setProperties(obj,nargin,varargin{:})
        end

    end

    methods (Access = protected)
        %% Common functions
        function setupImpl(obj)
            % Perform one-time calculations, such as computing constants
 
        
        end

        function Out = stepImpl(obj,LTSCFastSink,Damp,Setpoint,UpperEnd,Mix,DropSpeed)
            % Implement algorithm. Calculate y as a function of input u and
            % discrete states.

            %LTSCFastSink:0lock,1transport,2stop,3control;
            %Damp:0off,1on;
            
            %check input data is correct; right is flag=00; wrony is flag=01; 
            

            % Judging the operating mode 
            station = [LTSCFastSink Damp Setpoint UpperEnd Mix DropSpeed];
            if station(1) == 0                 %lock:0
                obj.CanBusData(1) =  station(3);
                obj.CanBusData(2) =  station(4);
                obj.CanBusData(3) =  station(6);
                obj.CanBusData(4) =  station(5);
                obj.CanBusData(5) =  station(1);
                obj.CanBusData(6) =  station(1);


            elseif station(1) == 1             %Transport:1
                %%%
            elseif station(1) == 2             %stop:2
                %%%
            elseif station(1) == 3             %control:3
                %%%
            elseif station(1) == 4             %DropSpeed:4
                %%%
            elseif station(1) == 1 ||station(2) == 1             %Damp:1

            elseif 123
                %%%
            
            else
                %%%%
                
            end

            








            % output control commend to HCU by can bus
            Out.Extended = uint8(1);
            Out.Length = uint8(8);
            Out.Remote = uint8(0);
            Out.Error = uint8(0);
            Out.ID = uint32(0x18ff9d31);
            Out.Timestamp = single(0);

            Out.Data = uint8(zeros([1 8]));
            Out.Data(1) = obj.CanBusData(1);
            Out.Data(2) = obj.CanBusData(2);
            Out.Data(3) = obj.CanBusData(3);
            Out.Data(4) = obj.CanBusData(4);
            Out.Data(5) = obj.CanBusData(5);
            Out.Data(6) = obj.CanBusData(6);
            Out.Data(7) = obj.CanBusData(7);
            Out.Data(8) = obj.CanBusData(8);

           
        end

        function resetImpl(obj)
            % Initialize / reset discrete-state properties
            obj.CanBusData = [0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00];

        end

        %% Backup/restore functions
        function s = saveObjectImpl(obj)
            % Set properties in structure s to values in object obj

            % Set public properties and states
            s = saveObjectImpl@matlab.System(obj);

            % Set private and protected properties
            %s.myproperty = obj.myproperty;
        end

        function loadObjectImpl(obj,s,wasLocked)
            % Set properties in object obj to values in structure s

            % Set private and protected properties
            % obj.myproperty = s.myproperty; 

            % Set public properties and states
            loadObjectImpl@matlab.System(obj,s,wasLocked);
        end



        %% Simulink functions
        function ds = getDiscreteStateImpl(obj)
            % Return structure of properties with DiscreteState attribute
            ds = struct([]);
        end

        function flag = isInputSizeMutableImpl(obj,index)
            % Return false if input size cannot change
            % between calls to the System object
            flag = false;
        end

        function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
            if strcmp(name,'CanBusData')
                sz = [1 8];
                dt = 'uint16';
                cp = false;
            else
                error(['Error: Incorrect State Name: ', name.']);
            end
        end

        
        function fixedout = isOutputFixedSizeImpl(~)
            %true = fixed size ;false = variable size;
            fixedout = true;
        end
        
        function out = isOutputComplexImpl(~)
            %true = fixed size ;false = variable size;
            out = false;
        end
        

        %define out put datatype
        function out = getOutputDataTypeImpl(obj)
            out = obj.OutputBusName;
        end

        function out = getOutputSizeImpl(obj)
            % Return size for each output port
            out = [1 1];

            % Example: inherit size from first input port
            % out = propagatedInputSize(obj,1);
        end

        function icon = getIconImpl(obj)
            % Define icon for System block
            icon = mfilename("class"); % Use class name
            icon = "Tractor lift API\n" + ...
                "-------------------------------------\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
            "|                                          |\n" + ...
                "-------------------------------------";
            % icon = "My System"; % Example: text icon
            % icon = ["My","System"]; % Example: multi-line text icon
            % icon = matlab.system.display.Icon("myicon.jpg"); % Example: image file icon
        end
    end

    methods (Static, Access = protected)
        %% Simulink customization functions
        function header = getHeaderImpl
            % Define header panel for System block dialog
            header = matlab.system.display.Header(mfilename("class"));
        end

        function group = getPropertyGroupsImpl
            % Define property section(s) for System block dialog
            group = matlab.system.display.Section(mfilename("class"));
        end
    end
end
